﻿package 
{
	//WEEK 04
	import flash.display.MovieClip;
	import flash.display.FrameLabel;
	import flash.events.*;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	
	public class GameDoc extends MovieClip{
		//w2 Menu Constants
		static public const MENU:String = "menu";
		static public const GAME:String = "game";
		static public const END:String = "end";
		
		//w3 Keys
		static public const LEFT:int = Keyboard.LEFT;
		static public const RIGHT:int = Keyboard.RIGHT;
		static public const DOWN:int = Keyboard.DOWN;
		static public const UP:int = Keyboard.UP;
				
		//w3 Movement Properties
		private var _power:int = 3;
		private var _leftPressed:Boolean = false;
		private var _rightPressed:Boolean = false;
		private var _upPressed:Boolean = false;
		private var _downPressed:Boolean = false;

		private var _characterNextX:int;
		private var _characterNextY:int;
		
		public function GameDoc(){
			//w2 setup frames of main movie to send an event when they are reached
			enumerateFrameLabels();
			addEventListener(MENU, setupMenu,false,0,true);
			addEventListener(GAME, setupGame,false,0,true);
			addEventListener(END, setupEnd,false,0,true);
		}
		//w2 added code to labelled key frames
		private function dispatchFrameEvent():void{
			stop();
			dispatchEvent(new Event(currentLabel));
		}
		//w2 finds the labelled key frames and adds the above code to them
		private function enumerateFrameLabels():void{
			for each (var label:FrameLabel in currentLabels)
				addFrameScript(label.frame-1, dispatchFrameEvent);
		}
		
		//w2 code run when hit menu label 
		private function setupMenu(e:Event){
			trace("setupMenu");
			test_button.button_text.text = "Start";
			test_button.addEventListener(MouseEvent.CLICK, aClick,false,0,true);
		}
		
		//w2 code run when hit game label
		private function setupGame(e:Event){
			end_button.button_text.text = "Win";
			end_button.addEventListener(MouseEvent.CLICK, bClick,false,0,true);
			// add keyboard listeners
			stage.addEventListener(KeyboardEvent.KEY_DOWN,onKDown,false,0,true);
			stage.addEventListener(KeyboardEvent.KEY_UP,onKUp,false,0,true);
			// add the main game loop listener
			addEventListener(Event.ENTER_FRAME,update,false,0,true);
			
		}
		//w2 code run when hit results label
		private function setupEnd(e:Event){
			//Remove Keyboard listeners and Enter frame listener
			stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKDown);
			stage.removeEventListener(KeyboardEvent.KEY_UP,onKDown);
			removeEventListener(Event.ENTER_FRAME,update);
			
			start_button.button_text.text = "Menu";
			start_button.addEventListener(MouseEvent.CLICK, cClick,false,0,true);
			
		}
		
		//w2 START Button Management Code		
		private function aClick(e:Event){
			gotoAndPlay(GAME);
		}
		private function bClick(e:Event){
			gotoAndPlay(END);
		}
		private function cClick(e:Event){
			gotoAndPlay(MENU);
		}
		//w2 END Button Management
		
		//w3 START Input Management
		private function onKDown(e:KeyboardEvent):void
		{
			switch(e.keyCode)
			{
				case LEFT: _leftPressed = true;
				break;
				case RIGHT: _rightPressed = true;
				break;
				case DOWN: _downPressed = true;
				break;
				case UP: _upPressed = true;
				break;
			}
		}
		
		private function onKUp(e:KeyboardEvent):void
		{
			switch(e.keyCode)
			{
				case LEFT: 
				_leftPressed = false;
				break;
				case RIGHT: 
				_rightPressed = false;
				break;
				case DOWN: 
				_downPressed = false;
				break;
				case UP: 
				_upPressed = false;
				break;
			}
		}
		
		
		//w3 END Input Management
		
		
		//w4 game cleanup functions
		private function cleanUpGame()
		{
			//Remove Keyboard listeners and Enter frame listener
			stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKDown);
			stage.removeEventListener(KeyboardEvent.KEY_UP,onKDown);
			removeEventListener(Event.ENTER_FRAME,update);
		}
		
		private function exitGame()
		{
			cleanUpGame();
			gotoAndPlay(END);
		}
		
		private function backToStart()
		{
			_characterNextX = 0;
			_characterNextY = 32;
		}
		//character.x -= power;	character.gotoAndPlay("left");
		//character.x += power;	character.gotoAndPlay("right");
		
		//changed w4 MAIN GAME LOOP
		private function update(e:Event)
		{
			handleKeyInput();
			checkCharacterCollision();
			movecharacter();
		}
		
		private function checkCharacterCollision()
		{
			// Bottom Left - check clip 
			if(mazeClip.hitTestPoint(_characterNextX+11,_characterNextY+28,true))
			{
				// if the next move position would hit 
				// would it if there was no x movement 
				if(mazeClip.hitTestPoint(character.x+11,_characterNextY+28,true))
				{
					_characterNextX = character.x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if(mazeClip.hitTestPoint(_characterNextX+11,character.y+28,true))
				{
					_characterNextY = character.y;
				}
				
			}
			// Bottom Right
			if(mazeClip.hitTestPoint(_characterNextX+24,_characterNextY+28,true))
			{
				// if the next move position would hit 
				// would it if there was no x movement 
				if(mazeClip.hitTestPoint(character.x+24,_characterNextY+28,true))
				{
					_characterNextX = character.x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if(mazeClip.hitTestPoint(_characterNextX+24,character.y+28,true))
				{
					_characterNextY = character.y;
				}

			}
			// Top Left
			if(mazeClip.hitTestPoint(_characterNextX+6,_characterNextY+4,true))
			{
				// if the next move position would hit 
				// would it if there was no x movement 
				if(mazeClip.hitTestPoint(character.x+6,_characterNextY+4,true))
				{
					_characterNextX = character.x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if(mazeClip.hitTestPoint(_characterNextX+6,character.y+4,true))
				{
					_characterNextY = character.y;
				}
			}
			// Top Right
			if(mazeClip.hitTestPoint(_characterNextX+24,_characterNextY+4,true))
			{
				// if the next move position would hit 
				// would it if there was no x movement 
				if(mazeClip.hitTestPoint(character.x+24,_characterNextY+4,true))
				{
					_characterNextX = character.x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if(mazeClip.hitTestPoint(_characterNextX+24,character.y+4,true))
				{
					_characterNextY = character.y;
				}
			}
			
			// start FIRES CHECK
			// check the fires
			// Bottom Left
			if(firesClip.hitTestPoint(_characterNextX+11,_characterNextY+28,true))
			{
				backToStart();
			}
			
			// Bottom Right
			if(firesClip.hitTestPoint(_characterNextX+24,_characterNextY+28,true))
			{
				backToStart();
			}
			// Top Left
			if(firesClip.hitTestPoint(_characterNextX+6,_characterNextY+4,true))
			{
				backToStart();
			}
			// Top Right
			if(firesClip.hitTestPoint(_characterNextX+24,_characterNextY+4,true))
			{
				backToStart();
			}
			// end FIRES CHECK
			
			// start EXIT CHECK
			// check exit
			// Bottom Left
			if(exitClip.hitTestPoint(_characterNextX+11,_characterNextY+28,true))
			{
				exitGame();
			}
			//Bottom Right
			if(exitClip.hitTestPoint(_characterNextX+24,_characterNextY+28,true))
			{
				exitGame();
			}
			// Top Left
			if(exitClip.hitTestPoint(_characterNextX+6,_characterNextY+4,true))
			{
				exitGame();
			}
			// Top Right
			if(exitClip.hitTestPoint(_characterNextX+24,_characterNextY+4,true))
			{
				exitGame();
			}
			//end EXIT CHECK
		}
		
		private function handleKeyInput()
		{
			// set the _next position to the current position
			_characterNextX = character.x;
			_characterNextY = character.y;

			if(_leftPressed)
			{
				_characterNextX = character.x - _power;
				trace(character.currentLabel);
				character.nextFrame();
				if(character.currentLabel != "left")
				{
					character.gotoAndStop("left");
				}

			}
			if(_rightPressed)
			{
				_characterNextX = character.x + _power;
				
				character.nextFrame();
				trace(character.currentLabel);
				if(character.currentLabel != "right")
				{
					character.gotoAndStop("right");
				}
			}
			if(_upPressed)
			{
				_characterNextY = character.y - _power;
			}
			if(_downPressed)
			{
				_characterNextY = character.y + _power;
			}
		}
		
		private function movecharacter()
		{
			if(_characterNextX>=0 && _characterNextX<=500)
			{
				character.x = _characterNextX;
			}
			if(_characterNextY>=0 && _characterNextY<=350)
			{
				character.y = _characterNextY;
			}
		}
	}

}